package com.jianguozh.difs.framework.utils;

import com.jianguozh.difs.framework.security.AuthenticatedUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 安全服务工具类
 *
 * @author hanrs
 * @email 4076130@qq.com
 * @since 2022/3/14 15:25
 */
@Slf4j
public class SecurityUtils {

    private static Random rand;

    static {
        rand = new Random();
    }

    /**
     * 获取用户账户
     **/
    public static String getUsername() {
        return null != getLoginUser() ? getLoginUser().getUsername() : "";
    }

    /**
     * 获取用户
     **/
    public static AuthenticatedUser getLoginUser() {
        try {
            return (AuthenticatedUser) getAuthentication().getPrincipal();
        } catch (Exception e) {
            log.debug("Current authentication is anonymousUser.");
            return null;
        }
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword     真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    /**
     * 是否为运维管理员
     *
     * @param userId 用户ID
     * @return 结果
     */
    public static boolean isAdmin(String userId) {
        return userId != null && "admin".equals(userId);
    }

    /**
     * 主管单位默认管理员账号
     *
     * @return
     */
    public static String getSuperiorAdmin() {
        return "superior-admin";
    }

    /**
     * 随机生成账号
     *
     * @param exists
     * @param num
     * @return
     */
    public static Set<String> radomUsername(Set<String> exists, int num) {
        Set<String> newSets = new HashSet<>();
        while (newSets.size() < num) {
            StringBuffer sb = new StringBuffer();
            for (int j = 1; j <= 5; j++) {
                if (j <= 2) { //前两位获取字母
                    sb.append(getLetter(97, 122, 105, 108, 111));
                } else { //后三位用数字
                    sb.append(getNum());
                }
            }
            newSets.add(sb.toString());
        }
        return newSets;
    }

    /**
     * 随机获取一个小写字母(排除指定字母)
     *
     * @param exclude
     * @return
     */
    public static char getLetter(int... exclude) {
        int start = 97, end = 122;
        int random = start + rand.nextInt(end - start + 1 - exclude.length);
        for (int ex : exclude) {
            if (random < ex) {
                break;
            }
            random++;
        }
        return (char) random;
    }


    /**
     * 随机获取一个小写字母
     */
    public static char getLetter() {
        char c = (char) getRadomInt(97, 122);
        return c;
    }

    /**
     * 随机获取一个大写字母
     */
    public static char getUpper() {
        char c = (char) getRadomInt(65, 90);
        return c;
    }

    /**
     * 随机获取一个0-9的数字
     */
    public static int getNum() {
        return getRadomInt(1, 9);
    }

    /**
     * 获取一个范围内的随机数字
     *
     * @return
     */
    public static int getRadomInt(int min, int max) {
        return rand.nextInt(max - min + 1) + min;
    }


    public static void main(String[] args) {
        Set<String> usernames = radomUsername(new HashSet<String>(), 4);
        System.out.println(usernames);
    }
}
